package com.example.demo.test3.controller;

import com.example.demo.test3.entity.Test3;
import com.google.gson.Gson;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.gson.io.GsonDeserializer;
import io.jsonwebtoken.security.Keys;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @ClassName JwtController
 * @Author lys4134
 * @Date 2021/05/25 16:19:35
 * @Description JwtController
 * @Version 1.0
 **/
@SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
@RestController
@RequestMapping("/jwt/")
public class JwtController {

    @GetMapping("getJwt")
    @ApiOperation("获取jwt")
    public String getJwt(String secretKey, String workId, String userName, String hospitalCode, String hospitalName){
        // create
//        String secretKey = "0123456789";
        String secretKey256 = DigestUtils.sha256Hex(secretKey); //sha256 保证key有足够的长度
        System.out.println(secretKey256);
        SecretKey key = Keys.hmacShaKeyFor(secretKey256.getBytes(StandardCharsets.UTF_8));
        String jws = Jwts.builder()
                .claim("uuid", UUID.randomUUID().toString())
                .claim("timestamp", System.currentTimeMillis())
                .claim("work_id", workId)
                .claim("user_name", userName)
                .claim("hospital_id", hospitalCode)
                .claim("hospital_name", hospitalName)
                .signWith(key)
                .compact();
        System.out.println("jwt = \n" + jws);


//        System.out.println("jwts = \n" + get360Token("0123456789"));


        //  parse
        String secretKey_1 = secretKey;
        String secretKey256_1 = DigestUtils.sha256Hex(secretKey_1);
        SecretKey key_1 = Keys.hmacShaKeyFor(secretKey256_1.getBytes(StandardCharsets.UTF_8));
        try{
            Jws<Claims> jwsClaim = Jwts.parserBuilder()
                    .deserializeJsonWith(new GsonDeserializer<>(new Gson()))
                    .setSigningKey(key_1)
                    .build()
                    .parseClaimsJws(jws);
            Claims body = jwsClaim.getBody();
            System.out.println(body);
        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }


        return jws;
    }

    public static void main(String[] args) {
        List<String> a = new ArrayList<>();
        a.add("1");a.add("3");a.add("5");
        List<String> b = new ArrayList<>();
        b.add("1");b.add("3");b.add("7");
        for (String aa :a){
            Iterator<String> iterator = b.iterator();
            while (iterator.hasNext()) {
                String platform = iterator.next();
                if (aa.equals(platform)){
                    iterator.remove();
                }
            }

        }
        System.out.println(b);
    }

}
